//
//  GZZBxqViewController.m
//  gangquOASystem
//
//  Created by michael on 2018/7/12.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "GZZBxqViewController.h"
#import "GDataDefines.h"

@interface GZZBxqViewController ()

@end

@implementation GZZBxqViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _type = @"no";
        self.view.backgroundColor = [UIColor whiteColor];

                [self loaddata];
self.editing = NO;
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"编辑"style:UIBarButtonItemStylePlain target:self action:@selector(rightAction1)];

//为导航栏添加右侧按钮2
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"保存"style:UIBarButtonItemStylePlain target:self action:@selector(rightAction2)];

NSArray *arr = [[NSArray alloc]initWithObjects:item1, item2, nil];

self.navigationItem.rightBarButtonItem = item1;


    // Do any additional setup after loading the view.
}
- (void)rightAction1{
_type = @"yes";
self.navigationItem.rightBarButtonItem  = nil;
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"保存"style:UIBarButtonItemStylePlain target:self action:@selector(rightAction2)];
self.navigationItem.rightBarButtonItem = item2;


}
- (void)rightAction2{
_type = @"yes";
NSString *json =[NSString stringWithFormat: @"{'Monday':'%@','Tuesday':'%@', 'Wednesday':'%@','Thursday':'%@','Friday':'%@','Saturday':'%@','Sunday':'%@'}",_Montextview.text,_Tuestextview.text,_Wednestextview.text,_Thurstextview.text,_Fritextview.text,_Saturtextview.text,_Suntextview.text];
NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

    NSArray * array =@[@{@"json":json},@{@"sName":userId}];
        [HttpTools postWithURL:@"exchassistant" arrays:array method:@"saveWorklog"  success:^(id JSON) {
         GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
        if ([message isEqualToString:@"20"]) {
            [self.view.window makeToast:@"保存成功"];
[self performSelector: @selector(popaction) withObject: nil afterDelay:1.0f];

}else{
        [self.view.window makeToast:@"保存失败"];

}
 } failure:^(NSError *error) {
        [self.view.window makeToast:@"保存失败"];
    }];

}
- (void)popaction{
//    [defaults removeObjectForKey:@"dic"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshData" object:nil]; //刷新发件箱，收件箱 数据

[self.navigationController popViewControllerAnimated:YES];

}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([_type isEqualToString:@"no"]) {
                return NO;

}else{
            return YES;

}

}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if ([_type isEqualToString:@"no"]) {
                return NO;

}else{
            return YES;

}

}
- (void)loaddata{

NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unId":self.fileId},@{@"userId":userId}];
[HttpTools postWithURL:@"exchassistant" arrays:array method:@"contentWorklog"  success:^(id JSON) {
NSLog(@"%@",JSON);
 NSData * data = [JSON dataUsingEncoding:NSUTF8StringEncoding];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
    //获取根节点（Users）
    GDataXMLElement *rootElement = [doc rootElement];
    //获取根节点下的节点（User）
    NSArray *rootData= [rootElement elementsForName:@"数据"];
    for (GDataXMLElement *subData in rootData) {
            GDataXMLElement *documentElement= [[subData elementsForName:@"文档"] objectAtIndex:0];
           _string = [documentElement stringValue];
                self.formArray = [_string componentsSeparatedByString:@"@@"];

            
            
        
            
        
    }
[self initview];


} failure:^(NSError *error) {
    }];

}
- (void)initview{
if(!_tableView){
 _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight-kTopHeight) style:UITableViewStylePlain];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.bounces = NO;
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 100;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row ==0){
 static NSString *cellid00 = @"cellid00";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid00];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid00];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

                NSString * allString = self.formArray[indexPath.row];
    NSArray * tempArray = [allString componentsSeparatedByString:@"##"];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0,KScreenWidth-20, 20)];
  label.textColor = [UIColor colorWithHexString:@"484848"];
label.text = tempArray.firstObject;
//label.attributedText = attrStr;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _Montextview = [[zhucetextview alloc]init];
              _Montextview.delegate = self;
    self.Montextview.text = tempArray.lastObject;

    _Montextview.frame = CGRectMake(10, label.bottom, (KScreenWidth-20), 80);
 
          [cell.contentView addSubview:_Montextview];

}
  
return cell;
}else if (indexPath.row ==1){
 static NSString *cellid01 = @"cellid01";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid01];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid01];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

                NSString * allString = self.formArray[indexPath.row];
    NSArray * tempArray = [allString componentsSeparatedByString:@"##"];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0,KScreenWidth-20, 20)];
  label.textColor = [UIColor colorWithHexString:@"484848"];
label.text = tempArray.firstObject;
//label.attributedText = attrStr;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _Tuestextview = [[zhucetextview alloc]init];
              _Tuestextview.delegate = self;
                 _Tuestextview.text = tempArray.lastObject;

    _Tuestextview.frame = CGRectMake(10, label.bottom, (KScreenWidth-20), 80);
 
          [cell.contentView addSubview:_Tuestextview];

}
  
return cell;
}else if (indexPath.row ==2){
 static NSString *cellid02 = @"cellid02";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid02];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid02];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

         NSString * allString = self.formArray[indexPath.row];
    NSArray * tempArray = [allString componentsSeparatedByString:@"##"];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0,KScreenWidth-20, 20)];
  label.textColor = [UIColor colorWithHexString:@"484848"];
label.text = tempArray.firstObject;
//label.attributedText = attrStr;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _Wednestextview = [[zhucetextview alloc]init];
              _Wednestextview.delegate = self;
    _Wednestextview.frame = CGRectMake(10, label.bottom, (KScreenWidth-20), 80);
 _Wednestextview.text= tempArray.lastObject;

          [cell.contentView addSubview:_Wednestextview];

}
  
return cell;
}else if (indexPath.row ==3){
 static NSString *cellid03 = @"cellid03";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid03];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid03];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

         NSString * allString = self.formArray[indexPath.row];
    NSArray * tempArray = [allString componentsSeparatedByString:@"##"];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0,KScreenWidth-20, 20)];
  label.textColor = [UIColor colorWithHexString:@"484848"];
label.text = tempArray.firstObject;
//label.attributedText = attrStr;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _Thurstextview = [[zhucetextview alloc]init];
              _Thurstextview.delegate = self;
    _Thurstextview.frame = CGRectMake(10, label.bottom, (KScreenWidth-20), 80);
  _Thurstextview.text = tempArray.lastObject;

          [cell.contentView addSubview:_Thurstextview];

}
  
return cell;
}else if (indexPath.row ==4){
 static NSString *cellid04 = @"cellid04";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid04];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid04];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

          NSString * allString = self.formArray[indexPath.row];
    NSArray * tempArray = [allString componentsSeparatedByString:@"##"];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0,KScreenWidth-20, 20)];
label.text = tempArray.firstObject;

  label.textColor = [UIColor colorWithHexString:@"484848"];
_Fritextview.text = tempArray.firstObject;
//label.attributedText = attrStr;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _Fritextview = [[zhucetextview alloc]init];
              _Fritextview.delegate = self;
    _Fritextview.frame = CGRectMake(10, label.bottom, (KScreenWidth-20), 80);
_Fritextview.text = tempArray.lastObject;

          [cell.contentView addSubview:_Fritextview];

}
  
return cell;
}else if (indexPath.row ==5){
 static NSString *cellid05 = @"cellid05";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid05];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid05];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

         NSString * allString = self.formArray[indexPath.row];
    NSArray * tempArray = [allString componentsSeparatedByString:@"##"];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0,KScreenWidth-20, 20)];
  label.textColor = [UIColor colorWithHexString:@"484848"];
label.text = tempArray.firstObject;
//label.attributedText = attrStr;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _Saturtextview = [[zhucetextview alloc]init];
              _Saturtextview.delegate = self;
    _Saturtextview.frame = CGRectMake(10, label.bottom, (KScreenWidth-20), 80);
                  _Saturtextview.text = tempArray.lastObject;

          [cell.contentView addSubview:_Saturtextview];

}
  
return cell;
}


else {
 static NSString *cellid06 = @"cellid06";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid06];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid06];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0,KScreenWidth-20, 20)];
 NSString * allString = self.formArray[indexPath.row];
    NSArray * tempArray = [allString componentsSeparatedByString:@"##"];
  label.textColor = [UIColor colorWithHexString:@"484848"];
label.text = tempArray.firstObject;
//label.attributedText = attrStr;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _Suntextview = [[zhucetextview alloc]init];
              _Suntextview.delegate = self;
    _Suntextview.frame = CGRectMake(10, label.bottom, (KScreenWidth-20), 80);
                  _Suntextview.text = tempArray.lastObject;

          [cell.contentView addSubview:_Suntextview];

}
  
return cell;
}
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
